package com.example.demo.controller;

import com.example.demo.config.AjaxResult;
import com.example.demo.model.ProductInfo;
import com.example.demo.model.UserInfo;
import com.example.demo.model.CartInfo;
import com.example.demo.service.ProductService;
import com.example.demo.util.ConstVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @RequestMapping("/get-all")
    public List<ProductInfo> getAllProducts() {
        return productService.getAllProducts();
    }

    @RequestMapping("/choose")
    public Object productChoose(String brand, int minPrice, int maxPrice) {
        return productService.productChoose(brand, minPrice, maxPrice);
    }

    @RequestMapping("/detail")
    public Object getProductDetail(int id) {
        return productService.getProductDetail(id);
    }

    @RequestMapping("/add")
    public Object addProduct(HttpServletRequest request, int pid) {
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(ConstVariable.USER_SESSION_KEY) == null) {
            return AjaxResult.fail(-1, "当前用户未登录，添加购物车失败！");
        }
        UserInfo userInfo = (UserInfo) session.getAttribute(ConstVariable.USER_SESSION_KEY);
        int uid = userInfo.getId();
        CartInfo cartInfo = new CartInfo();
        cartInfo.setUid(uid);
        cartInfo.setPid(pid);
        int result = productService.addProduct(cartInfo);
        if (result != 0) {
            return AjaxResult.success("购物车添加成功！", 1);

        } else {
            return AjaxResult.fail(-1, "数据库插入失败，购物车添加失败，请稍后再试！");
        }
    }

    @RequestMapping("/search")
    public Object searchProduct(String keyword) {
        return AjaxResult.success("搜索成功", productService.searchProduct(keyword));
    }



    @RequestMapping("/get-cart")
    public Object getMyCart(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(ConstVariable.USER_SESSION_KEY) == null) {
            return AjaxResult.fail(-1, "当前用户未登录，获取购物车失败！");
        }
        UserInfo userInfo = (UserInfo) session.getAttribute(ConstVariable.USER_SESSION_KEY);
        int uid = userInfo.getId();
        if (uid <= 0) {
            return AjaxResult.fail(401, "当前用户未登录");
        }
        return AjaxResult.success("获取购物车成功", productService.getMyCart(uid));
    }

    @RequestMapping("/up-cart")
    public Object updateMyCart(HttpServletRequest request, int pid, int quantity) {
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(ConstVariable.USER_SESSION_KEY) == null) {
            return AjaxResult.fail(-1, "当前用户未登录，更新购物车失败！");
        }
        UserInfo userInfo = (UserInfo) session.getAttribute(ConstVariable.USER_SESSION_KEY);
        int uid = userInfo.getId();
        CartInfo cartInfo = new CartInfo();
        cartInfo.setUid(uid);
        cartInfo.setPid(pid);
        cartInfo.setQuantity(quantity);
        if (uid <= 0) {
            return AjaxResult.fail(401, "当前用户未登录");
        }
        if (pid <= 0) {
            return AjaxResult.fail(-1, "商品不存在");
        }
        return AjaxResult.success("更新购物车成功", productService.updateMyCart(cartInfo));
    }

    @RequestMapping("/del-cart")
    public Object deleteMyCart(HttpServletRequest request, int pid) {
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(ConstVariable.USER_SESSION_KEY) == null) {
            return AjaxResult.fail(-1, "当前用户未登录");
        }
        UserInfo userInfo = (UserInfo) session.getAttribute(ConstVariable.USER_SESSION_KEY);
        int uid = userInfo.getId();
        CartInfo cartInfo = new CartInfo();
        cartInfo.setUid(uid);
        cartInfo.setPid(pid);
        if (uid <= 0) {
            return AjaxResult.fail(401, "当前用户未登录");
        }
        if (pid <= 0) {
            return AjaxResult.fail(-1, "商品不存在");
        }
        return AjaxResult.success("删除购物车成功", productService.delMyCart(cartInfo));

    }

}
